﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Carbon_Tracker
{
    public partial class frmReporteOM : Form
    {
        bool auditMode = false;
        public frmReporteOM()
        {
            InitializeComponent();
            DatabaseInterface.Connection cnx = new DatabaseInterface.Connection();
            BaseUnits.Builders.PowerPlantBuilder pbuilder = new BaseUnits.Builders.PowerPlantBuilder(cnx);
            foreach (BaseUnits.IPowerPlant pl in pbuilder.getPowerPlants("1000"))
            {
                this.cmbPowerPlants.Items.Add(pl);
            }
            this.auditMode = false;
        }

        public bool AuditMode
        {
            set { this.auditMode = value; } 
        }

        private void btnRun_Click(object sender, EventArgs e)
        {
            try
            {
                Cursor.Current = Cursors.WaitCursor;
                DateTime fromDate = dtpFrom.Value;
                DateTime toDate = dtpTo.Value;
                fromDate = fromDate.Date;
                toDate = toDate.Date;
                toDate = toDate.AddHours(23);

                if (this.auditMode)
                {
                    DialogResult result = MessageBox.Show("Audit mode can take a long time to execute/rAre you sure you wish to continue?", "Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                    if (result == System.Windows.Forms.DialogResult.Yes)
                    {

                    }
                }
                else
                {

                    BaseUnits.IPowerPlant plant = (BaseUnits.IPowerPlant)cmbPowerPlants.SelectedItem;
                    Formulas.DispatchReport report = new Formulas.DispatchReport();

                    Formulas.ReportData.unitDispatchDataTable table;
                    table = (Formulas.ReportData.unitDispatchDataTable)report.GetDispatchData(fromDate, toDate, plant, new TimeSpan(1, 0, 0, 0));

                    BaseUnits.Builders.GridBuilder gBuilder = new BaseUnits.Builders.GridBuilder(new DatabaseInterface.Connection());
                    BaseUnits.IPowerGrid grid = gBuilder.GetGrid(plant.PowerPlantId);

                    this.gridDispatch.DataSource = table;
                    this.gridDispatch.Refresh();
                    Formulas.DispatchOM dOM = new Formulas.DispatchOM(plant, grid, new DatabaseInterface.GeneralInfo(new DatabaseInterface.Connection()));

                    Formulas.BuildMargin bM = new Formulas.BuildMargin(grid);
                    IEnumerable enumerable = table.AsEnumerable();
                    //chart.DataBindTable(enumerable, "Date");

                    chart.Series.Clear();
                    chart.Series.Add("Outgoing");
                    chart.Series.Add("Incoming");

                    chart.DataSource = table;

                    chart.Series["Incoming"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Area;
                    chart.Series["Incoming"].XValueMember = "Date";
                    chart.Series["Incoming"].XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Date;
                    chart.Series["Incoming"].YValueMembers = "Incoming";

                    chart.Series["Outgoing"].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.Area;
                    chart.Series["Outgoing"].XValueMember = "Date";
                    chart.Series["Outgoing"].XValueType = System.Windows.Forms.DataVisualization.Charting.ChartValueType.Date;
                    chart.Series["Outgoing"].YValueMembers = "Outgoing";

                    chart.DataBind();


                    double OM = dOM.GetOMPeriod(fromDate, toDate);
                    txtOM.Text = Math.Round(OM, 3).ToString();
                    double BM = bM.GetBM(fromDate, toDate);
                    txtBM.Text = Math.Round(BM, 3).ToString();
                    Cursor.Current = Cursors.Default;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
        }

        private void frmReporteOM_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
        }
    }
}
